23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
27 map
<string
, long long> removeEmptyProteins(map
<string
, long long> &config
) {
28 map
<string
, long long> ans
;
29 for (map
<string
, long long>::iterator i
= config
.begin(); i
!= config
.end(); ++i
) {
30 const string
&protein
= i
->first
;
31 long long times
= i
->second
;
39 map
<string
, long long> mutate(map
<string
, string
> &mutations
, map
<string
, long long> &config
) {
40 map
<string
, long long> ans
;
41 for (map
<string
, long long>::iterator i
= config
.begin(); i
!= config
.end(); ++i
) {
42 const string
&protein
= i
->first
;
43 long long times
= i
->second
;
44 if (mutations
.count(protein
) > 0) {
45 const string
&newProtein
= mutations
[protein
];
46 ans
[ newProtein
] += times
;
48 ans
[ protein
] += times
;
54 int solveCase(map
<string
, string
> &mutations
, const map
<string
, long long> &initialConfig
, const map
<string
, long long> &finalConfig
, int bound
) {
55 if (initialConfig
== finalConfig
) return 0;
57 map
<string
, long long> config
= initialConfig
;
58 for (int k
= 1; k
<= bound
; ++k
) {
60 config
= mutate(mutations
, config
);
61 config
= removeEmptyProteins(config
);
63 // printf("after %d mutations config is:\n", k);
64 // for (map<string, long long>::iterator i = config.begin(); i != config.end(); ++i) {
65 // const string &protein = i->first;
66 // long long times = i->second;
67 // printf("%s -> %lld\n", protein.c_str(), times);
70 if (config
== finalConfig
) return k
;
77 int NM
, NI
, NC
, bound
;
78 while (cin
>> NM
>> NI
>> NC
>> bound
) {
79 if (NM
== 0 and NI
== 0 and NC
== 0 and bound
== 0) break;
81 bool deterministic
= true;
82 map
<string
, string
> mutations
;
83 for (int t
= 0; t
< NM
; ++t
) {
86 if (mutations
.count(k
) > 0 and mutations
[k
] != v
) {
87 deterministic
= false;
92 map
<string
, long long> initialConfig
, finalConfig
;
93 for (int i
= 0; i
< NI
; ++i
) {
94 string protein
; long long times
;
95 cin
>> protein
>> times
;
96 initialConfig
[protein
] = times
;
99 for (int i
= 0; i
< NC
; ++i
) {
100 string protein
; long long times
;
101 cin
>> protein
>> times
;
102 finalConfig
[protein
] = times
;
105 if (!deterministic
) {
106 printf("Protein mutations are not deterministic\n");
110 int ans
= solveCase(mutations
, initialConfig
, finalConfig
, bound
);
112 printf("Nostalgia for Infinity is doomed\n");
114 printf("Cure found in %d mutation(s)\n", ans
);